package com.koron.plan.web.patrolplan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.plan.patrol.dto.PatrolDto;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.plan.web.patrolplan.bean.query.PatrolPlanQuery;
import com.koron.plan.web.patrolplan.bean.vo.PatrolPlanDetailVO;
import com.koron.plan.web.patrolplan.bean.vo.PatrolPlanVO;
import com.koron.plan.web.patrolplan.service.PatrolPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author twcao
 * @title: PatrolPlanController
 * @projectName GDH_EAM_V2
 * @description: 巡视计划
 * @date 2021/10/278:40
 */
@RestController
@RequestMapping("/patrol/plan")
@Api(tags = "巡视计划")
public class PatrolPlanController {

    @Autowired
    private PatrolPlanService planService;

    @GetMapping("/selectOne")
    @ApiOperation("根据巡视计划id查询详情")
    @ApiImplicitParam(name = "planId", value = "巡视计划id", required = true, dataType = "String")
    public Response<PatrolPlanDetailVO> selectOne(@CasUser @ApiIgnore EamUser user, @RequestParam String planId) {
        return Response.succ(ADOConnection.runTask(user.getCurrDs(), planService, "queryById", PatrolPlanDetailVO.class, planId));
    }

    @GetMapping("/queryList")
    @ApiOperation("巡视计划列表")
    public Response<PageInfo<PatrolPlanVO>> queryList(@CasUser @ApiIgnore EamUser user, PatrolPlanQuery query) {
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<PatrolPlanVO> list = ADOConnection.runTask(user.getCurrDs(), planService, "queryList", List.class, query);
        return Response.succ(new PageInfo<>(list));
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("保存或更新巡视计划")
    public Response<PatrolPlanDetailVO> saveOrUpdate(@CasUser @ApiIgnore EamUser user, @Validated @RequestBody PatrolPlanDetailVO plan) {
        return Response.succ(ADOConnection.runTask(user.getCurrDs(), planService, "saveOrUpdate", PatrolPlanDetailVO.class, plan));
    }

    @GetMapping("/startOrStop")
    @ApiOperation("启用/停止巡视计划")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "巡视计划id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "startOrStop", value = "启动或停止标志，startOrStop = true，启用巡视计划。startOrStop = false，停用巡视计划", required = true, dataType = "Boolean")
    })
    public Response<PatrolPlanDetailVO> startOrStop(@CasUser @ApiIgnore EamUser user, @RequestParam String id, @RequestParam boolean startOrStop) {
        return Response.succ(ADOConnection.runTask(user.getCurrDs(), planService, "startOrStop", PatrolPlanDetailVO.class, id, startOrStop));
    }

    @GetMapping("/issue")
    @ApiOperation("下发巡视计划，生成工单")
    @ApiImplicitParam(name = "id", value = "巡视计划id", required = true, dataType = "String")
    public Response<PatrolPlanDetailVO> issue(@CasUser @ApiIgnore EamUser user, @RequestParam String id) {
        return Response.succ(ADOConnection.runTask(user.getCurrDs(), planService, "issue", PatrolPlanDetailVO.class, id));
    }

    @DeleteMapping("/deleteById")
    @ApiOperation("根据巡视点id删除巡视计划")
    @ApiImplicitParam(name = "planId", value = "巡视计划id", required = true, dataType = "String")
    public Response<Boolean> deleteById(@CasUser @ApiIgnore EamUser user, @RequestParam String planId) {
        return Response.succ(ADOConnection.runTask(user.getCurrDs(), planService, "hiddenById", Boolean.class, planId));
    }


    @GetMapping("/getPlanPatrolMsg")
    @ApiOperation("通过计划的Id,获取计划生成工单需要的所有信息")
    @ApiImplicitParam(name = "planId", value = "巡视计划id", required = true, dataType = "String")
    public Response<PatrolDto> getPlanPatrolMsg(@CasUser @ApiIgnore EamUser user, @RequestParam String planId, @RequestParam String planCreateTime) {
        return Response.succ(ADOConnection.runTask(user.getCurrDs(), planService, "getPlanPatrolMsg", PatrolDto.class, planId,planCreateTime));
    }
}
